package com.example.usbhidtool.utils

import android.content.Context
import android.os.Environment
import com.example.usbhidtool.hid.HidContants
import java.io.*
import java.security.MessageDigest


/**
 * 验机精灵帮助类
 */
object HidUtil {


    fun bytesToHexString(src: ByteArray?): String? {
        val stringBuilder = StringBuilder("")
        if (src == null || src.isEmpty()) {
            return null
        }
        for (i in src.indices) {
            val v: Int = src[i].toInt() and 0xFF
            val hv = Integer.toHexString(v)
            if (hv.length < 2) {
                stringBuilder.append(0)
            }
            stringBuilder.append(hv)
        }
        return stringBuilder.toString()
    }

    /**
     *获取验机精灵的本地目录
     */
    fun getHidDir(): String {
        val dir =
                Environment.getExternalStorageDirectory().absolutePath + File.separator + HidContants.DIR
        return dir
    }

    /**
     * 获取固件文件的路径
     */
    fun getFirmWarePath(): String {
        val path = getHidDir() + File.separator + HidContants.FIRM_WARE
        return path
    }

    /**
     * 获取固件MD5配置文件的MD5
     */
    fun getFirmWareMd5FilePath(): String {
        val path = getHidDir() + File.separator + HidContants.FIRM_WARE_CONFIG
        return path
    }

    /**
     * 获取质检App安卓包的路径
     */
    fun getApkPath(): String {
        val path = getHidDir() + File.separator + HidContants.APK_NAME
        return path
    }

    /**
     * 获取质检App IOS 包的路径
     */
    fun getIpaPath(): String {
        val path = getHidDir() + File.separator + HidContants.IPA_NAME
        return path
    }


    /**
     * 获取assets目录下文件的MD5
     */
    fun getAssertFileMD5(context: Context, fileName: String): String? {
        //获取文件大小
        try {
            val manager = context.assets
            val intpuStream = manager.open(fileName)
            val bins = BufferedInputStream(intpuStream)
            val buffer = ByteArray(1024)
            val digest: MessageDigest
            var len: Int
            try {
                digest = MessageDigest.getInstance("MD5")
                while (bins.read(buffer, 0, 1024).also { len = it } != -1) {
                    digest.update(buffer, 0, len)
                }
                bins.close()
            } catch (e: Exception) {
                e.printStackTrace()
                return null
            }
            return bytesToHexString(digest.digest())
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * 获取Sdcard下文件的MD5
     */
    fun getSdCardFileMd5(fileName: String): String? {
        //获取文件大小
        try {
            val file = File(fileName)
            val bins = BufferedInputStream(FileInputStream(file))
            val buffer = ByteArray(1024)
            val digest: MessageDigest
            var len: Int
            try {
                digest = MessageDigest.getInstance("MD5")
                while (bins.read(buffer, 0, 1024).also { len = it } != -1) {
                    digest.update(buffer, 0, len)
                }
                bins.close()
            } catch (e: Exception) {
                e.printStackTrace()
                return null
            }
            return bytesToHexString(digest.digest())
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun getFirmwareMd5Config(): String? {
        //获取文件大小
        try {
            val path = getFirmWareMd5FilePath()
            val file = File(path)
            var sb = StringBuilder()
            file.readLines().map {
                sb.append(it)
            }
            return sb.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }


}